<template>
	<view class="container">
		<image class="tui-bg__img" src="/static/images/mall/wallet/bg_balance_detail.png" ></image>
		<view class="tui-content__box">
			<view class="tui-amount__box">
				<image class="tui-amount__img" src="/static/images/mall/wallet/bg_balance_amount.png" mode="widthFix">
				</image>
					<view class="tui-amount">
						<view>{{bail}} <text class="tui-amount-unit">元</text></view>
					</view>
			</view>

			<view class="tui-btn__box">
				<view class="tui-btn__box-item" @tap="toBailLog">
					<tui-icon name="evaluate" color="#000"></tui-icon>
					<view class="tui-btn__box-item-text">记录</view>
				</view>
				
				<view class="tui-btn__box-item" @tap="switchRecharge">
					<tui-icon name="wealth"  color="#000"></tui-icon>
					<view class="tui-btn__box-item-text">充值</view>
				</view>
				
				<view class="tui-btn__box-item" @tap="switchwithdraw">
					<tui-icon name="wallet"  color="#000"></tui-icon>
					<view class="tui-btn__box-item-text">提现</view>
				</view>
			</view>
		</view>
		
		
		<tui-bottom-popup :show="recharge" :height="600" @close="switchRecharge">
			<view class="tui-modal-custom" style="padding: 60rpx 40rpx;;box-sizing: border-box;">
				<view style="text-align: center;margin-bottom: 80rpx;">保证金充值</view>
				<input type="text" placeholder="请输入充值金额" v-model="amount" style="text-align: center;margin-bottom: 40rpx;">
				<tui-button height="72rpx" :size="28"  shape="circle" @click="bindRecharge" style="margin-bottom: 40rpx;">立即充值</tui-button>
			</view>
		</tui-bottom-popup>
		
		<tui-bottom-popup :show="withdraw" :height="600" @close="switchwithdraw">
			<view class="tui-modal-custom" style="padding: 60rpx 40rpx;;box-sizing: border-box;">
				<view style="text-align: center;margin-bottom: 80rpx;">保证金提现</view>
				<input type="text" placeholder="请输入提现金额" v-model="withdraw_amount" style="text-align: center;margin-bottom: 40rpx;">
				<tui-button height="72rpx" :size="28"  shape="circle" @click="bindwithdraw" style="margin-bottom: 40rpx;">立即提现</tui-button>
			</view>
		</tui-bottom-popup>
	</view>
</template>

<script>
	import pay from '@/common/pay.js'
	export default {
		data() {
			return {
				bail: 0.00,
				amount: '',
				withdraw_amount:'',
				recharge: false,
				withdraw: false
			}
		},
		onLoad() {
			this.getBail()
		},
		methods: {
			getBail(){
				this.$request.get('storebail.getBail',{}).then(res=>{
					if(res.errno == 0){
						this.bail = res.data.bail
					}
				})
			},
		    switchRecharge(){
				this.recharge = !this.recharge
			},
			switchwithdraw(){
				this.withdraw = !this.withdraw
			},
			toBailLog(){
				uni.navigateTo({
					url:"baillog",
					success: (res) => {
						console.log(res)
					},
					fail: (res) => {
						console.log(res)
					}
				})
			},
			bindwithdraw(){
				const _this = this
				const amount = _this.withdraw_amount;
				console.log("amount: ",amount);
				if (amount == "" || amount * 1 < 0) {
					uni.showToast({
						title: '请填写正确的金额',
						icon: 'none'
					})
					return
				}
				uni.showModal({
					title: '请确认提现金额',
					content: '您本次提现 ' + amount + ' 元',
					confirmText: "确认",
					cancelText: "取消",
					success: function(res) {
						if (res.confirm) {
							_this.$request.post('storebail.withdraw',{
								amount: amount
							}).then(res=>{
								console.log("res: ",res);
								if(res.errno == 0){
									uni.navigateTo({
										url:"/pagesA/my/recharge/success"
									})
								}else{
									uni.showModal({
										title: '提示',
										content: res.msg,
										showCancel:false,
										success:(res)=>{
											
										}
									})
								}
							})
						}
					}
				});
			},
			async bindRecharge(e) {
				const _this = this
				const amount = _this.amount;
				console.log("amount: ",amount);
				if (amount == "" || amount * 1 < 0) {
					uni.showToast({
						title: '请填写正确的金额',
						icon: 'none'
					})
					return
				}
				uni.showModal({
					title: '请确认充值金额',
					content: '您本次充值 ' + amount + ' 元',
					confirmText: "确认支付",
					cancelText: "取消支付",
					success: function(res) {
						if (res.confirm) {
							_this.$request.post('storebail.recharge',{
								amount: amount
							}).then(res=>{
								console.log("res: ",res);
								if(res.errno == 0){
									console.log(res)
									const redirectUrl = "/pagesA/my/recharge/success";
									pay.heepay('storebail', amount, res.data.id, redirectUrl);
								}else{
									uni.showModal({
										title: '提示',
										content: res.msg,
										showCancel:false,
										success:(res)=>{
											
										}
									})
								}
							})
						}
					}
				});
			}
		}
	}
</script>

<style>
	.container {
		width: 100%;
		padding-bottom: 48rpx;
	}

	.tui-bg__img {
		width: 100%;
		height: 230rpx;
	}

	.tui-content__box {
		width: 100%;
		padding: 0 30rpx;
		box-sizing: border-box;
		position: relative;
		margin-top: -150rpx;
		z-index: 2;
	}

	.tui-amount__box {
		width: 100%;
		height: 280rpx;
		background: #FFFFFF;
		box-shadow: 0 10rpx 14rpx 0 rgba(0, 0, 0, 0.06);
		border-radius: 10rpx;
		position: relative;
		display: flex;
		justify-content: center;
		flex-direction: column;
		padding-left: 52rpx;
		box-sizing: border-box;
	}

	.tui-amount__img {
		width: 342rpx;
		height: 248rpx;
		position: absolute;
		right: 0;
		bottom: 0;
		z-index: 1;
	}

	.tui-amount {
		font-size: 56rpx;
		font-weight: 500;
		/* color: #4b70f5; */
		display: flex;
		align-items: center;
		position: relative;
		z-index: 10;
	}
	.tui-amount-unit{
		font-size: 32rpx;
	}

	.tui-badge {
		height: 34rpx;
		background: #FFE5E5;
		border-radius: 6rpx;
		font-size: 25rpx;
		font-weight: 400;
		color: #EB0909;
		display: flex;
		align-items: center;
		justify-content: center;
		margin-left: 12rpx;
	}

	.tui-scale__text {
		transform: scale(0.8);
		transform-origin: center center;
	}

	.tui-recharge__text {
		font-size: 28rpx;
		font-weight: 400;
		color: #888888;
		padding-top: 16rpx;
		padding-left: 12rpx;
		box-sizing: border-box;
	}

	.tui-badge__box {
		display: inline-flex;
		align-items: center;
		height: 42rpx;
		background-color: #FFDBDB;
		border-radius: 6rpx;
		overflow: hidden;
		margin-top: 50rpx;
	}

	.tui-badge__title {
		height: 42rpx;
		color: #fff;
		background-color: #EB0909;
		font-size: 24rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 0 10rpx;
	}

	.tui-badge__desc {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		font-weight: 400;
		color: #EB0909;
		padding: 0 10rpx;
	}

	.tui-explain__box {
		padding-top: 60rpx;
		font-weight: 400;
		color: #333333;
	}

	.tui-title {
		font-size: 30rpx;
		padding-bottom: 8rpx;
	}

	.tui-desc {
		font-size: 26rpx;
		padding-top: 12rpx;
	}

	.tui-btn__box {
		padding-top: 80rpx;
		display: flex;
		justify-content: space-between;
	}
	
	.tui-btn__box-item{
		width: 33%;
		text-align: center;
	}
	.tui-btn__box-item-text{
		font-size: 20rpx;
	}
	
</style>
